package com.newasia.management.services;/**
 * Created by mic on 2019/7/30.
 */


import com.newasia.commons.tool.exception.ValidateException;
import com.newasia.management.IManagementBrandService;

import com.newasia.management.constants.ManagementRetCode;
import com.newasia.management.converter.ItemBrandConverter;
import com.newasia.management.dal.item.entitys.ItemBrand;
import com.newasia.management.dal.item.entitys.ItemBrandExample;
import com.newasia.management.dal.item.entitys.ItemSpuExample;
import com.newasia.management.dal.item.persistence.ItemBrandMapper;
import com.newasia.management.dal.item.persistence.ItemSpuMapper;
import com.newasia.management.dto.productBrand.*;
import com.newasia.management.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;


@Slf4j
@Service
public class ManagementBrandImpl implements IManagementBrandService {

    @Autowired
    ItemBrandMapper itemBrandMapper;

    @Autowired
    ItemBrandConverter itemBrandConverter;

    @Autowired
    ItemSpuMapper itemSpuMapper;



    /*********************品牌**********************/
    /**
     * 创建品牌
     * @return
     * @param request
     */
    @Override
    public CreateBrandResponse createBrand(CreateBrandRequest request) {
        CreateBrandResponse response = new CreateBrandResponse();
        response.setCode(ManagementRetCode.SUCCESS.getCode());
        response.setMsg(ManagementRetCode.SUCCESS.getMessage());
        try{
            request.requestCheck();
            ItemBrandExample example = new ItemBrandExample();
            example.createCriteria().andBrandNameEqualTo(request.getBrandName());
            long count = itemBrandMapper.countByExample(example);
            if(count>0){
                throw new ValidateException(
                        ManagementRetCode.EXIST_ITEM_BRAND_NAME .getCode(),
                        ManagementRetCode.EXIST_ITEM_BRAND_NAME .getMessage()
                );
            }

            ItemBrand itemBrand = itemBrandConverter.req2brand(request);
            Date date = new Date();

            itemBrand.setCreateTime(date);
            itemBrand.setUpdateTime(date);

            itemBrandMapper.insert(itemBrand);
        }catch (Exception e){
            log.error("ManagementItemCatServiceImpl.getItemCat occur Exception :"+e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
            e.printStackTrace();
            return response;
        }

        return response;
    }

    /**
     * 删除品牌
     * @return
     * @param brandId
     */
    @Override
    public DeleteBrandResponse deleteBrand(Long brandId) {
        DeleteBrandResponse response = new DeleteBrandResponse();
        response.setCode(ManagementRetCode.SUCCESS.getCode());
        response.setMsg(ManagementRetCode.SUCCESS.getMessage());
        try{
            ItemSpuExample example = new ItemSpuExample();
            example.createCriteria().andBrandIdEqualTo(brandId);
            long count = itemSpuMapper.countByExample(example);
            if(count>0){
                throw new ValidateException(
                        ManagementRetCode.EXIST_ITEM_BRAND_GOODS .getCode(),
                        ManagementRetCode.EXIST_ITEM_BRAND_GOODS .getMessage()
                );
            }
            itemBrandMapper.deleteByPrimaryKey(brandId);
        }catch (Exception e){
            log.error("ManagementItemCatServiceImpl.getItemCat occur Exception :"+e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
            e.printStackTrace();
            return response;
        }
        return response;
    }


    /**
     * 修改品牌
     * @return
     * @param brandId
     */
    @Override
    public ChangeBrandResponse changeBrand(Long brandId, ChangeBrandRequest request) {
        ChangeBrandResponse response = new ChangeBrandResponse();
        response.setCode(ManagementRetCode.SUCCESS.getCode());
        response.setMsg(ManagementRetCode.SUCCESS.getMessage());
        ItemBrand itemBrand=itemBrandConverter.changereq2brand(request);
        try{
            ItemBrandExample example = new ItemBrandExample();
            ItemBrandExample.Criteria criteria = example.createCriteria();
            criteria.andBrandIdNotEqualTo(brandId);

            criteria.andBrandNameEqualTo(request.getBrandName());

            long count = itemBrandMapper.countByExample(example);

            if(count>0){
                throw new ValidateException(
                        ManagementRetCode.EXIST_ITEM_BRAND_NAME .getCode(),
                        ManagementRetCode.EXIST_ITEM_BRAND_NAME .getMessage()
                );
            }
            Date date = new Date();
            itemBrand.setBrandId(brandId);
            itemBrand.setUpdateTime(date);

            log.info(itemBrand.toString());
            itemBrandMapper.updateByPrimaryKeySelective(itemBrand);
        }catch (Exception e){
            log.error("ManagementItemCatServiceImpl.getItemCat occur Exception :"+e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
            e.printStackTrace();
            return response;
        }

        return response;
    }


    /**
     * 获取品牌列表
     * @return
     */
    @Override
    public GetBrandResponse getBrandList(String key) {
        GetBrandResponse response = new GetBrandResponse();
        response.setCode(ManagementRetCode.SUCCESS.getCode());
        response.setMsg(ManagementRetCode.SUCCESS.getMessage());
        try{
            ItemBrandExample example = new ItemBrandExample();
            if(key!=null){
                log.info(key);
                example.createCriteria().andBrandNameLike("%"+key+"%");
            }
            List<ItemBrand> brands = itemBrandMapper.selectByExample(example);
            response.setBrandDtoList(itemBrandConverter.item2dto(brands));
        }catch (Exception e){
            log.error("ManagementItemCatServiceImpl.getItemCat occur Exception :"+e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
            e.printStackTrace();
            return response;
        }
        return response;
    }






}
